<?php
function updateCheckboxes($transId, $checked)
{
    $publications = getPublicationsByTransaction($transId);
    $summarisedList = array();
    $unsummarisedList = array();
    foreach ($publications as $publication) {
        $term = 'ID = ' . $publication;
        if (in_array($publication, $checked)) {
            $summarisedList[] = $term;
        } else {
            $unsummarisedList[] = $term;
        }
    }
    $summarisedTerms = implode(' OR ', $summarisedList);
    $unsummarisedTerms = implode(' OR ', $unsummarisedList);
    updateSummarised($summarisedTerms, 1);
    updateSummarised($unsummarisedTerms, 0);
}
function modelUpdateDatabase($transId, &$post)
{
    global $db;
    $publication = $post['publication'];
    $summary = $db->escape($post['noticeSummary']);
    if (empty($publication)) {
        $publication = array();
    }
    updateCheckboxes($transId, $publication);
    updateSummary($transId, $summary);
}
function modelPersonInfo($personInfo)
{
    $personId = $personInfo['id'];
    $lastName = $personInfo['lastName'];
    $firstName = $personInfo['firstName'];
    $akaName = $personInfo['akaName'];
    $neeName = $personInfo['neeName'];
    $formerName = $personInfo['formerName'];
    $deathDate = $personInfo['deathDate'];
    $birthDate = $personInfo['birthDate'];
    $age = $personInfo['age'];
    $ageMeasure = $personInfo['ageMeasure'];
    $country = $personInfo['country'];
    $cityTown = $personInfo['cityTown'];
    $formerCityTown = $personInfo['formerCityTown'];
    $charity = $personInfo['charity'];
    $funeralHome = $personInfo['funeralHome'];
    if (empty($lastName)) {
        $lastName = 'Unknown';
    }
    if (empty($firstName)) {
        $firstName = 'Unknown';
    }
    if (empty($akaName)) {
        $akaName = 'Unknown';
    }
    if (empty($neeName)) {
        $neeName = 'Unknown';
    }
    if (empty($formerName)) {
        $formerName = 'Unknown';
    }
    if ($deathDate === '0000-00-00 00:00:00') {
        $deathDate = 'Unknown';
    } else {
        $deathDate = date('d F Y', strtotime($deathDate));
    }
    if ($birthDate === '0000-00-00 00:00:00') {
        $birthDate = 'Unknown';
    } else {
        $birthDate = date('d F Y', strtotime($birthDate));
    }
    if (empty($age)) {
        $age = 'Unknown';
    }
    switch ($ageMeasure) {
        case 'y':
            $ageMeasure = 'years';
            break;
        default:
            $ageMeasure = '';
    }
    if (empty($country)) {
        $country = 'Unknown';
    }
    if (empty($cityTown)) {
        $cityTown = 'Unknown';
    }
    if (empty($formerCityTown)) {
        $formerCityTown = 'Unknown';
    }
    if (empty($charity)) {
        $charity = 'Unknown';
    }
    if (empty($funeralHome)) {
        $funeralHome = 'Unknown';
    }
    return viewPersonInfo($personId, $lastName, $firstName, $akaName, $neeName, $formerName, $deathDate, $birthDate, $age, $ageMeasure, $country, $cityTown, $formerCityTown, $charity, $funeralHome);
}
function modelFormNewspapers($personId, $listOfPapers, $noticeSummary)
{
    $newspapers = '';
    $newspapersInfo = array();
    $papers = explode(' | ', $listOfPapers);
    foreach ($papers as $index => $paperName) {
        $newspaper = '';
        $newspaperInfo = getNewspaperInfo($personId, $paperName);
        foreach ($newspaperInfo as $publication) {
            $newspaper .= viewNewspaperCheckbox($paperName, $publication);
        }
        $newspapers .= $newspaper;
    }
    return $newspapers;
}
function modelEmailNewspapers($personId, $listOfPapers, $noticeSummary)
{
    $newspapers = '';
    $newspapersInfo = array();
    $papers = explode(' | ', $listOfPapers);
    foreach ($papers as $index => $paperName) {
        $newspaperInfo = getNewspaperInfo($personId, $paperName);
        foreach ($newspaperInfo as $publication) {
            $newspapers .= viewNewspaperEmail($paperName, $publication);
        }
    }
    return '<p>' . $newspapers . '</p>';
}
function modelEmailButton($success, $processed)
{
    if (!empty($success)) {
        if (empty($processed)) {
            $email = viewEmailButton();
        } else {
            $email = viewProcessedEmailButton();
        }
    } else {
        $email = viewUnpaidEmailButton();
    }
    return $email;
}
function modelFormSummary($transId, $personId, $fullName, $date, $personDetails, $request, $newspapers, $summary, $success, $processed)
{
    $email = modelEmailButton($success, $processed);
    return viewFormSummary($transId, $personId, $fullName, $date, $personDetails, $request, $newspapers, $summary, $email);
}
function modelSummary($transInfo, $updateSummary)
{
    global $db;
    global $html;
    global $email;
    $transId = intval($transInfo['id']);
    $personId = intval($transInfo['txnData1']);
    $papers = $db->escape($transInfo['txnData2']);
    $toEmail = $db->escape($transInfo['txnData3']);
    $success = intval($transInfo['success']);
    $processed = intval($transInfo['processed']);
    $personInfo = getPersonInfo($personId);
    $personId = $personInfo['id'];
    $lastName = $personInfo['lastName'];
    $firstName = $personInfo['firstName'];
    $fullName = $firstName . ' ' . $lastName;
    $date = date('d F Y');
    $personDetails = modelPersonInfo($personInfo);
    if ($updateSummary) {
        $request = '<p>Request for ' . $papers . '</p>';
        $newspapers = modelFormNewspapers($personId, $papers, $noticeSummary);
        $noticeSummary = $personInfo['noticeSummary'];
        $summary = viewNoticeSummary($noticeSummary);
        $text = modelFormSummary($transId, $personId, $fullName, $date, $personDetails, $request, $newspapers, $summary, $success, $processed);
    } else {
        $newspapers = modelEmailNewspapers($personId, $papers, $noticeSummary);
        $noticeSummary = $personInfo['noticeSummary'];
        $summary = viewEmailNoticeSummary($noticeSummary);
        $text = viewEmailSummary($transId, $personId, $fullName, $date, $personDetails, $newspapers, $summary);
        $email->setToEmail($toEmail);
        $email->setFromName('A Memory Tree Order');
        $email->setFromEmail('order@amemorytree.co.nz');
        $email->setSubject('Notice Summary from ' . $papers . ' for ' . $fullName);
        $email->setMessage($text);
        $email->send();
        updateProcessed($transId, TRUE);
    }
    return $text;
}
?>